package com.anyou.mall.goo.goodsAudit.service.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import com.anyou.common.core.domain.ApiResult;
import com.anyou.common.utils.DateUtils;
import com.anyou.mall.goo.goods.domain.GooGoods;
import com.anyou.mall.goo.goods.dto.GoodsSearchParams;
import com.anyou.mall.goo.goods.mapper.GooGoodsMapper;
import com.anyou.mall.goo.goodsAudit.dto.GoodsAuditSearchParams;
import com.anyou.mall.goo.goodsAudit.vo.GoogdsDetailVo;
import com.anyou.mall.goo.goodsAudit.vo.SelectGoodsSpecsVo;
import com.anyou.mall.goo.goodsAudit.vo.SelectSpecsInfoVo;
import com.anyou.mall.goo.goodsSku.domain.GooGoodsSku;
import com.anyou.mall.goo.goodsSku.mapper.GooGoodsSkuMapper;
import com.anyou.mall.parts.domain.PlaPartsClassify;
import com.anyou.mall.parts.mapper.PlaPartsClassifyMapper;
import com.anyou.mall.sho.mapper.CarPartsShopAuditMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.anyou.mall.goo.goodsAudit.mapper.goodsAuditMapper;
import com.anyou.mall.goo.goodsAudit.domain.goodsAudit;
import com.anyou.mall.goo.goodsAudit.service.IgoodsAuditService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 商品审核Service业务层处理
 *
 * @author ruoyi
 * @date 2022-03-25
 */
@Service
public class goodsAuditServiceImpl extends ServiceImpl<goodsAuditMapper, goodsAudit> implements IgoodsAuditService
{
    @Autowired
    private goodsAuditMapper goodsAuditMapper;

    @Autowired
    private GooGoodsMapper gooGoodsMapper;

    @Autowired
    private PlaPartsClassifyMapper plaPartsClassifyMapper;

    @Autowired
    private GooGoodsSkuMapper goodsSkuMapper;

    //汽配商审核
    @Autowired
    private CarPartsShopAuditMapper carPartsShopAuditMapper;

    /**
     * 查询商品审核
     *
     * @param goodsId 商品审核主键
     * @return 商品审核
     */
    @Override
    public goodsAudit selectgoodsAuditByGoodsId(Long goodsId)
    {
        return goodsAuditMapper.selectgoodsAuditByGoodsId(goodsId);
    }

    /**
     * 查询商品审核列表
     *
     * @param goodsAudit 商品审核
     * @return 商品审核
     */
    @Override
    public List<goodsAudit> selectgoodsAuditList(GoodsSearchParams goodsAudit)
    {
        List<goodsAudit> list = goodsAuditMapper.selectList(goodsAudit.queryWrapper());
        list.forEach(e ->{
            if(StringUtils.isNotBlank(e.getCategoryPath())){
                List<Integer> idList = new ArrayList<>();
                List<String> stringList = Arrays.asList(e.getCategoryPath().split(","));
                stringList.forEach(id -> idList.add(Integer.parseInt(id)));
                List<PlaPartsClassify> plaPartsClassifyList = plaPartsClassifyMapper.selectList(new LambdaQueryWrapper<PlaPartsClassify>().in(PlaPartsClassify::getPartsClassifyId, idList));
                e.setCategoryNameStr(plaPartsClassifyList.stream().map(PlaPartsClassify::getPartsClassifyName).collect(Collectors.joining(",")));

                //库存
                /*List<GooGoodsSku> skuList = goodsSkuMapper.selectList(new LambdaQueryWrapper<GooGoodsSku>().eq(GooGoodsSku::getGoodsId,e.getGoodsId()).eq(GooGoodsSku::getDelFlag,"0"));
                long stockNum = skuList.stream().mapToLong(GooGoodsSku::getGoodsStock).sum();
                e.setStockNum(stockNum);*/

            }
            //店铺
            /*String shopName = carPartsShopAuditMapper.selectShopName(e.getCarPartsShopId());
            e.setCarPartsShopName(shopName);*/
        });
        return list;
    }

    /**
     * 新增商品审核
     *
     * @param goodsAudit 商品审核
     * @return 结果
     */
    @Override
    public int insertgoodsAudit(goodsAudit goodsAudit)
    {
        goodsAudit.setCreateTime(DateUtils.getNowDate());
        return goodsAuditMapper.insertgoodsAudit(goodsAudit);
    }

    /**
     * 修改商品审核
     *
     * @param goodsAudit 商品审核
     * @return 结果
     */
    @Override
    public int updategoodsAudit(goodsAudit goodsAudit)
    {
        goodsAudit.setUpdateTime(DateUtils.getNowDate());
        return goodsAuditMapper.updategoodsAudit(goodsAudit);
    }

    /**
     * 批量删除商品审核
     *
     * @param goodsIds 需要删除的商品审核主键
     * @return 结果
     */
    @Override
    public int deletegoodsAuditByGoodsIds(Long[] goodsIds)
    {
        return goodsAuditMapper.deletegoodsAuditByGoodsIds(goodsIds);
    }

    /**
     * 删除商品审核信息
     *
     * @param goodsId 商品审核主键
     * @return 结果
     */
    @Override
    public int deletegoodsAuditByGoodsId(Long goodsId)
    {
        return goodsAuditMapper.deletegoodsAuditByGoodsId(goodsId);
    }

    @Override
    public ApiResult selectGoodsSpecs(Long goodsId) {
        List<SelectSpecsInfoVo> selectSpecsInfoVos = goodsAuditMapper.selectSpecsInfo(goodsId);

        // 查询规格明细
        /*List<SelectSpecsInfoVo> selectSpecsInfoVos = goodsAuditMapper.selectSpecsInfo(goodsId);*/
        selectSpecsInfoVos.forEach(e->{
            e.setListSelectSpecsValueInfoVo(goodsAuditMapper.selectSpecsValueInfo(e.getSpecsId(),goodsId));
        });


        // 将规格明细和规格SKU信息存放
        SelectGoodsSpecsVo selectGoodsSpecsVo = new SelectGoodsSpecsVo();
        selectGoodsSpecsVo.setListSelectSpecsInfoVo(selectSpecsInfoVos);
        selectGoodsSpecsVo.setListSelectSpecsSkuInfoVo(goodsAuditMapper.selectSpecsSkuInfo(goodsId));

        //查询
        List<String> contentIds = selectSpecsInfoVos.stream().map(model->{
            return model.getSpecsName();
        }).collect(Collectors.toList());
        String SpecsNames = StringUtils.join(Arrays.asList(contentIds), "、");
        selectGoodsSpecsVo.setSpecs(SpecsNames);

        GooGoods gooGoods = gooGoodsMapper.selectById(goodsId);
        selectGoodsSpecsVo.setGoodsName(gooGoods.getGoodsName());

        return ApiResult.success(selectGoodsSpecsVo);
    }

    @Override
    public ApiResult GoogdsDetail(Long goodsId) {
        //查询商品信息
        GoogdsDetailVo googdsDetailVo = goodsAuditMapper.GoogdsDetail(goodsId);
        //查询规格信息
        List<SelectSpecsInfoVo> selectSpecsInfoVos = goodsAuditMapper.selectSpecsInfo(goodsId);
        selectSpecsInfoVos.forEach(e->{
            e.setListSelectSpecsValueInfoVo(goodsAuditMapper.selectSpecsValueInfo(e.getSpecsId(),goodsId));
        });

        //组装数据返回
        //商品规格 规格值
        googdsDetailVo.setListSelectSpecsInfoVo(selectSpecsInfoVos);
        //商品sku
        googdsDetailVo.setListSelectSpecsSkuInfoVo(goodsAuditMapper.selectSpecsSkuInfo(goodsId));
        //商品参数
        googdsDetailVo.setListGoogdsDetail(goodsAuditMapper.selectGoodsParameter(goodsId));
        //店铺信息
        googdsDetailVo.setShopNameListVo(gooGoodsMapper.selectShopName(googdsDetailVo.getCarPartsShopId()));
        return ApiResult.success(googdsDetailVo);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ApiResult updateAuditStatus(Long [] goodsId, String auditStatus, String auditRemark ) {
        for (Long id : goodsId) {
            LambdaUpdateWrapper<goodsAudit> updateWrapper = new LambdaUpdateWrapper<>();
            updateWrapper.eq(goodsAudit::getGoodsId,id)
                    .set(goodsAudit::getAuditStatus,auditStatus)
                    .set(goodsAudit::getAuditRemark,auditRemark);
            goodsAuditMapper.update(null,updateWrapper);
        }
        return ApiResult.success();
    }
}
